//
//  NSKVONotifying_Person.m
//  KVO
//
//  Created by Hello Cai on 2021/7/14.
//  Copyright © 2021 Hello Cai. All rights reserved.
//

#import "NSKVONotifying_Person.h"

@implementation NSKVONotifying_Person

//TARGETS->Build Phases->Compile Sources移除NSKVONotifying_Person，不参与编译
//以下用伪代码的方式辅助理解苹果KVO的实现机制

- (void)setAge:(int)age
{
    _NSSetIntValueAndNotify();
}

void _NSSetIntValueAndNotify()
{
    [self willChangeValueForKey:@"age"];
    [super setAge:age];
    [self didChangeValueForKey:@"age"];
}

- (void)didChangeValueForKey:(NSString *)key
{
    // 通知监听器，某某属性值发生了改变
    [oberser observeValueForKeyPath:key ofObject:self change:nil context:nil];
}

// 屏蔽内部实现，隐藏了NSKVONotifying_MJPerson类的存在
- (Class)class
{
    return [MJPerson class];
}

- (void)dealloc
{
    // 收尾工作
}

- (BOOL)_isKVOA
{
    return YES;
}

@end
